package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;

import java.util.List;

import static javafx.beans.binding.Bindings.select;

public class CartServiceImpl implements CartService {
    private CartDao cartDao = new CartDaoImpl();
    @Override
    public void add(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public Cart find(Integer uid, int goodId) {
        return cartDao.select(uid,goodId);
    }

    @Override
    public void modify(Cart cart) {
        cartDao.update(cart);
    }

    @Override
    public List<Cart> find(Integer uid) {
       List<Cart> cartList =  cartDao.select(uid);
       //遍历购物车信息，通过购物车中的商品id，查找商品，封装购物车数据
        if (cartList != null) {
            GoodsService goodsService = new GoodsServiceImpl();
            for (Cart cart : cartList) {
                Goods goods = goodsService.findById(cart.getPid());//通过购物车表中的商品id(pid)来找到对应的商品
                cart.setGoods(goods);//调用购物车的setGoods，为goods(商品)赋值
            }
        }
        return cartList;
    }

    @Override
    public void remove(Integer uid, int goods_id) {
        cartDao.delete(uid,goods_id);
    }

    @Override
    public void clear(Integer uid) {
        cartDao.deleteAll(uid);
    }
}
